import { NgFor, NgIf } from '@angular/common';
import { Component, ElementRef } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { NzButtonComponent, NzButtonModule } from 'ng-zorro-antd/button';
import { NzDividerModule } from 'ng-zorro-antd/divider';
import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
import { NzEmptyModule } from 'ng-zorro-antd/empty';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzMenuModule } from 'ng-zorro-antd/menu';
import { NzModalModule } from 'ng-zorro-antd/modal';
import { NzTableModule, NzTrDirective } from 'ng-zorro-antd/table';
import { Column, MaterialSummary, ProductSummary } from '../../type';
import { CustomBaseComponent } from '../custom-base/custom-base.component';
import { FieldSelectorComponent } from '../field-selector/field-selector.component';
import { MaterialSummaryService, TcService } from '../tc.service';


@Component({
  selector: 'app-material-summary',
  standalone: true,
  imports: [
    NzTableModule, NzTrDirective, NgFor, NzDividerModule, NzFormModule, NzButtonComponent,
    NzInputModule, NzModalModule, NgIf, NzButtonModule, FormsModule, NzMenuModule, NzDropDownModule,
    NzModalModule, FieldSelectorComponent, NzEmptyModule

  ],
  templateUrl: '../custom-base/custom-base.component.html',
  styleUrl: '../custom-base/custom-base.component.less'
})
export class MaterialSummaryComponent extends CustomBaseComponent<MaterialSummary> {


  getExportUrl(): string {
    let filter = `rank_desc = 1`;
    return `/export-excel/export/海关备案物料汇总表.xlsx?filter=${encodeURIComponent(filter)}`;
  }

  getTreeField(): keyof MaterialSummary {
    return 'material_part_number';
  }

  constructor(elementRef: ElementRef, route: ActivatedRoute, public service: MaterialSummaryService) {
    super(elementRef);

    // this.itemId = route.snapshot.params['itemId'];
    // if (!this.itemId)
    //   this.error = `参数 itemId 不允许为空`;

  }


  getColumns(): Column<MaterialSummary>[] {

    let columns: Column<MaterialSummary>[] = [
      { title: '物料料号', field: 'material_part_number' },
      { title: '产线BOM编号（CRR）', field: 'project_line_bom' },
      { title: '发布时间', field: 'date_released' },
      { title: '物料名称', field: 'material_name' },
      { title: '材质规格/尺寸', field: 'material_spec_size' },
      { title: '物料单位重量', field: 'material_unit_weight' },
      { title: '可生产产品数量', field: 'producible_quantity' },
      { title: '单片耗用量', field: 'single_chip_consumption' },
      { title: '单位', field: 'unit' },
      { title: '净重', field: 'weight' },
      { title: '耗损', field: 'depletion' },
      { title: '有形损耗', field: 'material_loss' },
      { title: '无形损耗', field: 'intangible_loss' },
      { title: '来料方式', field: 'incoming_material_method' },
      { title: '物料属性', field: 'material_properties' },
      { title: '物料类别', field: 'material_category' },
      { title: '替代料编号', field: 'alt_material_number' },
      { title: '入口序号/批号', field: 'entr_serial_number' },
      { title: '工厂', field: 'product_shipment' },
      { title: '部门/联系人电话', field: 'factory_tel' },
      { title: '大类', field: 'big_classname' },
      { title: '备案项目名称', field: 'project_name' },
      { title: '用途', field: 'usage' },
      { title: '最终成品编码', field: 'projectcode_id_last' },
      { title: '成品尺寸（mm）', field: 'project_size_yc' },
      { title: '成品单片净重', field: 'project_weight' },
    ];

    return columns;
  }
  getDefaultSearchFields(): (keyof MaterialSummary)[] {
    return ['material_part_number', 'material_name', 'material_spec_size', 'entr_serial_number',
      'product_shipment', 'project_name', 'usage', 'projectcode_id_last', 'project_size_yc'
    ];
  }
  getLatestDataItems(): Promise<MaterialSummary[]> {
    return this.service.getLatestMaterialSummary();
  }
  searchDataItems(searchText: string, searchFields: (keyof MaterialSummary)[]): Promise<MaterialSummary[]> {
    return this.service.searchMaterialSummary(searchText, searchFields);
  }

  getHistoryDataItems(dataItem: MaterialSummary): Promise<MaterialSummary[]> {
    return this.service.getHistoryMaterialSummary([dataItem.item_id]);
  }

}
